// +----------------------------------------------------------------------
// | bossHongshi [鸿时互娱管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.hongshihuyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wgh
// +----------------------------------------------------------------------
const QRCode = require('qrcode');
module.exports = class extends think.cmswing.center {
  async __before() {
    await super.__before();
    // 判断是否登陆
    // await this.weblogin();
    if (!this.is_login) {
      // 判断浏览客户端
      if (this.isMobile) {
        // 手机端直接跳转到登录页面
        return this.redirect('/center/public/login');
      } else {
        return this.redirect('/cmswing/error/login');
      }
    }
  }
  // 我的邀请码
  async indexAction() {
	//const uid = this.get('uid') || this.user.uid;
	const userInfo = await this.model('member').find(this.user.uid);
    this.assign('userInfo', userInfo);
    
	let toDataURLPromise  = think.promisify(QRCode.toDataURL);
	//let url = 'https://a27yed.mlinks.cc/Ab87?roomid='+userInfo.username;
	let url = 'http://www.hongshihuyu.com/bdj/?i='+userInfo.game_id+'&t=2&r=&c=1&m=';
	
	const qrcode = await  toDataURLPromise(url);
    
    this.assign('qrcode',qrcode);
	this.meta_title = '我的邀请码';
    // 判断浏览客户端
    if (this.isMobile) {
      if (this.isAjax('get')) {
        for (const v of data.data) {
          v.create_time = this.moment(v.create_time).format('lll');
        }
        return this.json(data);
      } else {
        this.active = 'user/index';
        return this.display(this.mtpl());
      }
    } else {
      return this.display();
    }
  }

};